import pandas as pd
import Data
import sql_server
import threading
import os
import win32com.client

# Dataframe设置2位小数点
pd.set_option('display.precision', 2)

sql_conn = None
data_conn = None


def sql():
    global sql_conn
    sql_conn = sql_server.main()


def data():
    global data_conn
    data_conn = Data.main()


# 创建线程对象
thread1 = threading.Thread(target=sql)
thread2 = threading.Thread(target=data)

# 启动线程
thread1.start()
thread2.start()

# 等待线程结束
thread1.join()
thread2.join()


Summary_Table = pd.concat([data_conn, sql_conn], axis=1)
output = Summary_Table.reindex(columns=[
    '一级','二级','三级','四级','一级告警','二级告警','三级告警','四级告警',
    '力维总量','数据库C接口','C接口',' ',
    '维谛总量','数据库维谛接口','API',
])
output.replace('////', '/', inplace=True)
print(len(output))

try:
    # 获取 Excel 应用程序对象
    excel = win32com.client.GetActiveObject('Excel.Application')
    # 遍历所有打开的工作簿
    for workbook in excel.Workbooks:
        if workbook.Name == '3.0动环告警数量统计.xlsx':
            # 关闭指定的工作簿
            workbook.Close(SaveChanges=False)  # 如果需要保存更改，改为 SaveChanges=True
            print('成功关闭工作簿: 3.0动环告警数量统计.xlsx')
            break
except:
    pass
output.to_excel(f'{os.path.join(os.path.expanduser("~"), "Desktop")}\\3.0动环告警数量统计.xlsx',
                sheet_name="sheet1",
                index=True,
                header=True)
print('转换成功！！！')
xlsx = os.system(f"start {os.path.join(os.path.expanduser('~'), 'Desktop')}\\3.0动环告警数量统计.xlsx")
